package main

import (
	"encoding/json"
	"fmt"
	"time"

	"gitee.com/zibianqu/fyzdb"
	"gitee.com/zibianqu/fyzdb/connection/mysql"
	"gitee.com/zibianqu/fyzdb/dsn"
)

func main() {
	config := dsn.MysqlCfg()
	config.SetDsn("root:root@tcp(127.0.0.1:3306)/fyzdb?charset=utf8mb4&parseTime=true")
	mysqlConn := mysql.Mysql()
	mysqlConn.InitDB(config)
	db := mysqlConn.GetDb()
	defer func() {
		_ = db.Close()
	}()
	session := fyzdb.Seesion().BeginTrans()
	sessionId := session.GetSessionId()
	a := fyzdb.Table("sys_user").SetSessionId(sessionId).Where("UserId", 100076893570827317).
		Update(map[string]string{"Nickname": "Lua22"})

	var tmp any
	tc := fyzdb.Table("sys_user").Log()
	tc.SetSessionId(sessionId).Where("UserId", ">", 100076893570827317, true).Where("IsValid=1").First(&tmp)
	fmt.Println(tc.GetLogSql())
	fmt.Println(tmp)

	bc := fyzdb.Table("sys_user")
	b := bc.SetSessionId(sessionId).Where("UserId", "=", 100088837530632306, true).
		Update(map[string]string{"Nickname": "aaa1"})
	fmt.Println(bc.GetLogSql())
	if b == 0 || a == 0 {
		session.Rollback()
	} else {
		session.Commit()
	}

	fmt.Println(a, b)
	save := make(map[string]any)
	save["PostsId"] = 7222247067349942271
	save["UserId"] = 7221376778797842432
	save["Title"] = "ddfd"
	save["Cover"] = ""
	save["Type"] = 1
	save["Images"] = ""
	save["Content"] = "aaaaaaaaaa"
	c, _ := json.Marshal(map[string]string{"Cover": "", "Video": ""})
	save["Video"] = c
	save["Audio"] = ""
	save["Ctime"] = time.Now().Unix()
	save["Mtime"] = time.Now().Unix()
	rs, _ := fyzdb.Table("user_posts").Insert(save)
	fmt.Println(rs)

	rs1 := fyzdb.Table("user_posts").Where("PostsId", 7222247067349942271).Get()
	fmt.Println(rs1)

}
